package com.witframework.web.extremetable;


import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.AbstractCell;
import org.extremecomponents.table.core.TableModel;
import org.extremecomponents.table.view.html.ColumnBuilder;



/**
 * 限定长度的显示列.
 * 显示的字符长度是由format指定的,如:<code>format=20</code>
 * @author wuxinyang
 */
public class LimitDisplayCell extends AbstractCell {

    @Override
	public String getExportDisplay(TableModel model, Column column) {
    	return column.getPropertyValueAsString();
    }
    @Override
	public String getHtmlDisplay(TableModel model, Column column) {    	
        ColumnBuilder columnBuilder = new ColumnBuilder(column);        
        columnBuilder.tdStart();
        columnBuilder.tdBody(getCellValue(model, column));
        columnBuilder.tdEnd();
        if(column.isFirstColumn())
        	return ">"+columnBuilder.toString();
        return columnBuilder.toString();
    }
    @Override
	protected String getCellValue(TableModel model, Column column) {
        String column_value= column.getValueAsString();
        String ext_str1,ext_str2;
        String display_str;
        int indexBeg=column_value.indexOf(">");
        int indexEnd=column_value.indexOf("<",indexBeg);
        if(indexBeg>-1&&indexEnd>-1){
        	ext_str1=column_value.substring(0,indexBeg+1);
        	ext_str2=column_value.substring(indexEnd);
        	display_str=column_value.substring(indexBeg+1,indexEnd);
        }else if(indexBeg>-1&&indexEnd<0){
        	return column_value;
        }else{
        	ext_str1="";
        	ext_str2="";
        	display_str=column_value;

        }
        int  displayCount=0;//显示长度
        try{
        displayCount=Integer.parseInt(column.getFormat().trim());
        }catch(Exception e){}

        if(displayCount==0) displayCount=100;
        String limit_value=limitString(display_str,displayCount);
        return ext_str1+limit_value+ext_str2;

    }
    private String limitString(String display_str,int displayCount){

    	if(display_str==null) return "";
    	String limit_value;
    	display_str=display_str.trim();
    	if(displayCount==0)
    		return display_str;
        if(display_str.length()<displayCount){
        	limit_value=display_str;
        }else{
        	if(!StringUtils.isAsciiPrintable(display_str)){
        		displayCount=displayCount/2;
        	}
        	limit_value=display_str.substring(0,displayCount);	        
        }
        return "<span title='"+addBlank(display_str,50)+"'>"+limit_value+"</span>";
        
    }
    private String addBlank(String src,int l){
    	int cl=l;
    	StringBuffer newsrc=new StringBuffer(src);
    	while(newsrc.length()>cl){
    		newsrc.insert(cl,"\n");
    		cl=cl+l+1;
    	}
    	return newsrc.toString();
    }
    public static void main(String[] args) {

    	String column_value= "<A href=\"www.com\" style=\"border:0px\">我说你不是猪,但是没有人相信.</A>";
    	//column_value= "<A href=\"www.com\" style=\"border:0px\"/>I say you are not a pig, but no one believe.</A>";
    	column_value= "<input type=\"checkbox\" name=\"itemlist\" value=\"4028e4350a697726010a697bdb6b0002\" style=\"border:0px\"/>";
    	//column_value="我说你不是猪a，但是没有人相信!";
    	column_value=null;
    	LimitDisplayCell lc=new LimitDisplayCell();
        Column c=new Column(null);
        c.setValue(column_value);
        String x=lc.getCellValue(null,c);
        System.out.println(x);

	}
}
